"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ArticleSer_1 = __importDefault(require("../../services/ArticleSer"));
const infoerr_1 = require("../middleware/infoerr");
exports.default = {
    getTopHeat(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield ArticleSer_1.default.getTopHeat();
            if (data instanceof Object)
                return (0, infoerr_1.info)(res, "ok", data);
            return (0, infoerr_1.err)(res, "位置错误");
        });
    },
    getTopTime(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield ArticleSer_1.default.getTopTime();
            if (data instanceof Object)
                return (0, infoerr_1.info)(res, "ok", data);
            return (0, infoerr_1.err)(res, "位置错误");
        });
    },
    getTopGift(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield ArticleSer_1.default.getTopGift();
            if (data instanceof Object)
                return (0, infoerr_1.info)(res, "ok", data);
            return (0, infoerr_1.err)(res, "位置错误");
        });
    },
    getAlllimt(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield ArticleSer_1.default.getAlllimit(req.query);
            if (data instanceof Object)
                return (0, infoerr_1.info)(res, "ok", data);
            return (0, infoerr_1.err)(res, "获取文章失败");
        });
    },
    getFindTitle(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if (!req.query || !req.query.title || !req.query.x || !req.query.y)
                    return (0, infoerr_1.err)(res, "查询条件错误");
                const findtitles = {
                    title: req.query.title,
                    x: +req.query.x,
                    y: +req.query.y,
                };
                const data = yield ArticleSer_1.default.getFindTitle(findtitles);
                if (data instanceof Object) {
                    data;
                    let str = req.query.title;
                    // const reg1 = new RegExp(str, "gi");
                    const reg2 = new RegExp(str, "g");
                    const ddd = data.rows.map((item) => {
                        if (str.toUpperCase() === item.author.toUpperCase()) {
                            item.author = `<font style="color:#994dcd;">${item.author}</font>`;
                            return item;
                        }
                        const title = item.title.replace(reg2, `<span style="color:red;">${str}</span>`);
                        const titles = item.titles.replace(reg2, `<span style="color:red;">${str}</span>`);
                        const author = item.author.replace(reg2, `<font style="color:red;">${str}</font>`);
                        item.title = title;
                        item.titles = titles;
                        item.author = author;
                        return item;
                    });
                    data.rows = ddd;
                    return (0, infoerr_1.info)(res, "ok", data);
                }
                return (0, infoerr_1.err)(res, "未知错误");
            }
            catch (error) {
                next(error);
            }
        });
    },
    add(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const $ = req.body;
                if (!$.author ||
                    !$.browesNum ||
                    !$.comment ||
                    !$.content ||
                    !$.createdTime ||
                    !$.fontlength ||
                    !$.label ||
                    !$.title ||
                    !$.titles)
                    return (0, infoerr_1.err)(res, "参数有误", $);
                const data = yield ArticleSer_1.default.add($);
                console.log(data);
                if (data)
                    return (0, infoerr_1.info)(res, "ok", data);
                return (0, infoerr_1.err)(res, "插入失败");
            }
            catch (error) {
                next(error);
            }
        });
    },
    findpk(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if (!req.query.id || Object.is(+req.query.id, NaN)) {
                    return (0, infoerr_1.err)(res, "id错误", { id: req.query.id });
                }
                if (typeof req.query.id === "string") {
                    const data = yield ArticleSer_1.default.findpk(req.query.id);
                    if (!data)
                        return (0, infoerr_1.err)(res, `找不到id为:${req.query.id}的文章`);
                    let browesNum = +data.browesNum;
                    console.log("前", browesNum);
                    browesNum++;
                    console.log("后", browesNum);
                    (0, infoerr_1.info)(res, "ok", data);
                    browesNum = String(browesNum);
                    const result = yield ArticleSer_1.default.update(+req.query.id, { browesNum });
                    console.log(result);
                    return;
                }
                return (0, infoerr_1.err)(res, "未知错误");
            }
            catch (error) {
                next(error);
            }
        });
    },
    Alllabel(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = (yield ArticleSer_1.default.Alllabel());
            const data = result.map((item) => {
                if (item.label) {
                    return item.label;
                }
            });
            const set = new Set(data);
            set.delete(undefined);
            return (0, infoerr_1.info)(res, "ok", [...set]);
        });
    },
};
